Add onchain gas cost to trades and orders APIs - #4540
Conversation
|
Reminder: Please update the DB Readme and comment whether migrations are reversible (include rollback scripts if applicable).
Resolved |
|
Reminder: Please consider backward compatibility when modifying the API specification.
Caused by: |
|
Claude finished @jmg-duarte's task in 4m 13s —— View job PR Review: Add on-chain gas cost to trades and orders APIsI reviewed the diff against I left 3 inline comments:
The doc-comment mismatches (1 & 2) read like leftovers from an earlier quote-based design — worth fixing since they directly contradict the OpenAPI and the implementation. Minor (non-blocking)
|
There was a problem hiding this comment.
Code Review
This pull request implements tracking and persistence of actual on-chain gas costs for settlements, trades, and orders, including database migrations, query updates, and OpenAPI documentation. The reviewer feedback highlights that the docstrings for the gas_cost fields in both the OrderMetadata and Trade models are incorrect and misleading, as they describe the value as an estimated cost derived from quotes rather than the actual on-chain gas cost.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
MartinquaXD
left a comment
There was a problem hiding this comment.
Main concern is the error prone settlement <> trade association via log indices and the fact that this estimate can be very much off. Was that discussed with the solver / frontend team?
| WHERE s.block_number = page.block_number | ||
| AND s.log_index > page.log_index |
There was a problem hiding this comment.
I think deciding to not store the tx_hash in the trades table was a blunder back in the day which leads to this ugly code again and again. Also unless I'm missing something this code does not handle trades from multiple settlements in the same block correctly.
WDYT about introducing the tx_hash column on the trades table to simply join on that? Since we can already get the association using the log index this data can even be fully backfilled using a DB migration alone.
There was a problem hiding this comment.
For what it's worth. Same-block different-tx case actually works fine imo. the LATERAL JOIN picks the right settlement per trade and the inner subquery counts the right divisor, I did a dry run for multiple settles within a block case.
The case that does mis-attribute is one tx calling settle() multiple times.
Not sure if this is applicable for now. But in theory, each Settlement event row ends up with the full receipt's gas_used (find_settlement_trace_and_callers only returns the first settle frame in the trace), so per-event splitting sums to 2x the real cost across the tx's trades.
No solver does this afaik, but tx_hash on trades sidesteps the question entirely. So +1 on the direction 🚀
This comment was marked as outdated.
This comment was marked as outdated.
|
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
jmg-duarte
left a comment
There was a problem hiding this comment.
trying out commenting the PR myself so i can have claude address these
|
Claude finished @jmg-duarte's task in 6m 2s —— View job PR Review: Add on-chain gas cost to trades and orders APIsI re-reviewed the current state of the branch against Verified correct:
Findings (2 inline):
Known limitations (equal-split can be inaccurate for mixed-complexity batches; a
|
|
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
The settlement observer already reads `gas_used` and `effective_gas_price` from the transaction receipt to build its metrics. Store them on the `settlements` row (columns added in V116) so that consumers can attribute a real on-chain gas cost to settlements. Only settlements observed after this is deployed get values, historical rows stay `NULL`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Expose the settlement gas cost attributed to each fill as `gasCost` (native token wei) on the trades and order-detail endpoints: - a trade gets its settlement's `gas_used * effective_gas_price` split equally across the trades settled in the same transaction - an order gets the sum over all of its fills `null` when the underlying settlement has no recorded gas, i.e. settlements observed before V116 was deployed. Both values are computed in SQL so they come back in the same round-trip as the rest of the order/trade data. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
70a833c to
0063759
Compare
Description
The removal of UCP means that we no longer have fees and gas costs info in a single place, meaning the FE must calculate them; the issue comes when one notices that we don't keep track of gas costs for settlements (meaning trades and orders don't either).
This PR adds gas cost tracking, when settlements get indexed, they will now be indexed with the gas cost.
The PR does not do backfills on purpose, that must be done as a separate process.
Changes
How to test
Sepolia staging is currently suspended with this code there, please do a trade and review the UI using this link
https://jmgd-ucp.explorer-dev-dxz.pages.dev/
Under costs and fees you should see this:
